package Function.ItemUse;

import FCRendering.FCMapAnimationConstant;
import Function.FCMutilPlayCache;
import VWorldGameStudio.*;
import interfaces.IPlayerSlideCallback;
import io.reactivex.schedulers.Schedulers;
import utils.ClickHelper;

import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

import static FCRendering.FCRMaster.*;

public class ToolCowboyRope extends AItemUse implements IPlayerSlideCallback {
    ClickHelper clickHelper = new ClickHelper(1000);

    @Override
    public ItemUseResult directUse() {
        if(clickHelper.canClick()) {
            fcv.closeAllShowWindw();
            FCMap map = mapLib.getMapOfName(player1.getMapname());
            String aniKey = FCMapAnimationConstant.UP_SHOUMapAni;
            int x = player1.getPlayer1x();
            int y = player1.getPlayer1y();
            if (player1.getActionDir() == 2) {
                y += 1;
                aniKey = FCMapAnimationConstant.RIGHT_SHOUMapAni;
            } else if (player1.getActionDir() == 1) {
                y -= 3;
                aniKey = FCMapAnimationConstant.LEFT_SHOUMapAni;
            } else if (player1.getActionDir() == 3) {
                x -= 3;
                aniKey = FCMapAnimationConstant.UP_SHOUMapAni;
            } else if (player1.getActionDir() == 0) {
                x += 1;
                aniKey = FCMapAnimationConstant.DOWN_SHOUMapAni;
            }
            FCMapAnimation animation = mapAnimationLibrary.getMapAnimationByName(aniKey);
            if(animation != null){
                animation.resetAnimation();
                String xyKey = x+"*"+y;
                map.dynamicAddMapAnimation(aniKey,xyKey);
                animation.setVisiable(1);
            }
            Schedulers.io().scheduleDirect(new Runnable() {
                @Override
                public void run() {
                    fcrMaster.setPlayerSlideCallback(ToolCowboyRope.this);
                    fcrMaster.slideMoveNDistance(3);
                }
            },300, TimeUnit.MILLISECONDS);

        }
        return new ItemUseResult();
    }

    @Override
    public ItemUseResult useInMapTarget(int x, int y) {
        return directUse();
    }

    @Override
    public ItemUseResult useForMonsterTarget(FCBattleOrder battleOrder, FCMonster m) {
        fcv.getMessage().add("什么也没发生！");
        fcv.showMessage();
        return new ItemUseResult().useless();
    }

    @Override
    public ItemUseResult useForPlayerTarget(FCPlayerInfo p) {
        fcv.getMessage().add("什么也没发生！");
        fcv.showMessage();
        return new ItemUseResult().useless();
    }

    @Override
    public ItemUseResult useForTankTarget(FCTank t) {
        fcv.getMessage().add("什么也没发生！");
        fcv.showMessage();
        return new ItemUseResult().useless();
    }

    @Override
    public void onSlideToTarget(boolean isOk) {
        if(isOk) {
            FCMutilPlayCache.gamePlayers.forEach(new BiConsumer<String, FCPlayerInfo>() {
                @Override
                public void accept(String s, FCPlayerInfo playerInfo) {
                    if (playerInfo.getMapname().equals(player1.getMapname())) {
                        if (playerInfo.getPlayer1CoorX() == fcv.getSlideTargetMapX()
                                && playerInfo.getPlayer1CoorY() == fcv.getSlideTargetMapY()) {
                            if(!playerInfo.getAliasUID().equals(gameSave.HOST_UID)){
                                fcv.setBindingPlayerName(playerInfo.getName());
                                fcv.setBindingPlayerHostId(playerInfo.getAliasUID());
                            }
                        }
                    }
                }
            });
        }
    }
}
